package jwf.fcgi;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

public class FCGXBeginRequestBody {
    byte roleB1;
    byte roleB0;
    byte flags;
    byte[] reserved = new byte[5]; // length = 5

    public static FCGXBeginRequestBody makeBeginRequestBody(InputStream in)
	    throws IOException {
	FCGXBeginRequestBody body = new FCGXBeginRequestBody();
	byte[] buff = new byte[FCGXConstants.FCGIBeginReqBodyLen];
	int l = in.read(buff, 0, FCGXConstants.FCGIBeginReqBodyLen);
	if (l != FCGXConstants.FCGIBeginReqBodyLen) {
	    return null;
	}
	body.roleB1 = buff[0];
	body.roleB0 = buff[1];
	body.flags = buff[2];
	System.arraycopy(buff, 3, body.reserved, 0, 5);
	return body;
    }

    public String toString() {
	StringBuffer sb = new StringBuffer();
	sb.append("roleB1:").append(roleB1).append("\t");
	sb.append("roleB0:").append(roleB0).append("\t");
	sb.append("flags:").append(flags).append("\t");
	sb.append("reserved:").append(Arrays.toString(reserved));
	return sb.toString();
    }

    public int getRole() {
	return ((roleB1 & 0xFF) << 8) + (roleB0 & 0xFF);
    }
}
